#include "Tick_IRQ.h"

Ticker ticker100ms;
Ticker ticker40ms;
Ticker ticker25ms;
Ticker ticker3s;
Ticker ticker1m;
Ticker wea_updata_tic;

u8 oled_flg = 0;
u8 rssi_flg = 0;
u8 led_color_t = 0;
u8 wea_updata_t = 0;

void Ticker_init()
{
  ticker25ms.attach_ms(25,ms25_tic);
  ticker40ms.attach_ms(40,ms40_tic);
  ticker100ms.attach_ms(100,ms100_tic);
  ticker3s.attach(3,s3_tic);
  ticker1m.attach(60,m1_tic);
}


void ms25_tic()
{
  oled_flg = 1;
}
void ms40_tic()
{
  led.second(pcf.second);
  #ifndef DEBUG
  {
    led.write();
  }
  #endif

}
void ms100_tic()
{
  if(high_freq_flg)
  {
    Wire.setClock(200000);
    pcf.read_time();
    Wire.setClock(10000000);
  }
  else
    pcf.read_time();
  if(pcf.hour == 0)
  {
    if(pcf.min == 0 && pcf.second == 0)
    {
      pcf.read_day();
      ui.wea_updata_flg = 1;
    }
  }
  // led.time(pcf.hour,pcf.min);
  adc.fresh();
  
}

void s3_tic()
{
  if(rssi_flg&&!ui.menu_page)
  {
    ui.rssi_show_mode = !ui.rssi_show_mode;
    ui.Rssi_refresh(112);
  }
  if(ui.night_dis_t<11)
  { 
    ui.night_dis_t ++;
  }
  if(ui.night_dis_t ==10)
  {
    if(ui.menu_page == 10)
      ui.menu_page_return_flg = 1;
    if(ui.night_shutdown_flg && ui.sys_wake_flg)
    {
      if(ui.sleep_hour<ui.wake_hour)
      {
        if(pcf.hour>=ui.sleep_hour&&pcf.hour<ui.wake_hour)
          ui.shutdown_flg = 1;
      }
      else if(pcf.hour>=ui.sleep_hour||pcf.hour<ui.wake_hour)
        ui.shutdown_flg = 1;
    }

  }
}

void m1_tic()
{
  if(ui.son_moon_mode)
    ui.son_moon_x ++;
  else
    ui.son_moon_x --;
  if(ui.son_moon_x==0||ui.son_moon_x==25)
    ui.son_moon_mode = !ui.son_moon_mode;
  wea_updata_t ++;
  if(wea_updata_t ==20)
  {
    wea_updata_t = 0;
    ui.wea_updata_flg = 1;
  }
  led_color_t ++;
  if(led_color_t == 60)
  {
    led_color_t = 0;
    ui.led_color_set_flg = 1;
  }
}

void wea_updata()
{
  u8 buf;
  buf = love_y>>3;
  if(!buf)
    buf = 1;
  if(love_mode)
    love_y+=buf;
  else
    love_y-=buf;
  if(love_y<0)
  {
    love_mode = 1;
    love_y = 0;
  }
  else if(love_y>49)
  {
    love_mode = 0;
    love_y = 49;
  }

  oled.choose_clr(56,0,15,8);
  oled.BMP(56,love_y,15,15,love,1);
  oled.choose_refresh(56,0,15,8);
}
